package com.itheima.mp.controller;

import cn.hutool.core.bean.BeanUtil;
import com.itheima.mp.domain.dto.PageDTO;
import com.itheima.mp.query.UserQuery;
import com.itheima.mp.service.IUserService;
import com.itheima.mp.vo.UserVO;
import demain.dto.UserFormDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.itheima.mp.domain.User;

import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "用户管理接口")
@RestController
@RequestMapping("/users")
@Slf4j
@RequiredArgsConstructor
public class UserController {
    //注入 服务
//    private IUserService userService;
    private final IUserService userService;

//    public UserController(IUserService userService) {
//        this.userService = userService;
//    }

    @ApiOperation("新增用户接口01")
    @PostMapping
    public void saveUser(@RequestBody UserFormDTO userFormDTO) {
        log.info("----新增用户信息1111111111111");
        //粘贴
        User user = BeanUtil.copyProperties(userFormDTO, User.class);
        userService.save(user);
    }

    @ApiOperation("新增用户接口02")
    @DeleteMapping("{id}")
    public void deleteUserById(@ApiParam("用户id") @PathVariable("id") Long id) {
        log.info("----删除2222222222222");
        userService.removeById( id);
    }
    @ApiOperation("id查询用户03-改进")
    @GetMapping("{id}")
    public UserVO searchUserById(@ApiParam("用户id") @PathVariable("id") Long id) {
        log.info("----根据id查询用户333333333333");
//        User byId = userService.getById(id);
//        System.out.println(byId);

        return userService.queryUserAndAddressById(id); //调用service
//        return BeanUtil.copyProperties(byId, UserVO.class);

    }

    @ApiOperation("id批量查询用户04")
    @GetMapping
//    @GetMapping("{ids}")
    public List<UserVO> QuerysearchUserById(@ApiParam("用户id集合") @RequestParam("ids") List<Long> ids) {
        log.info("-----根据id查询用户4444444444");
//        List<User> users = userService.listByIds(ids);
//        return BeanUtil.copyToList(users, UserVO.class);
        return userService.queryUserAndAddressByIds(ids); //调用service
    }
    //根据用户id修改用户余额 要去service SQL
    @ApiOperation("根据id修改用户余额05")
    @PutMapping("{id}/deduction/{money}")
    public void deductionMoneyById(
            @ApiParam("用户id") @PathVariable("id") Long id,
            @ApiParam("要扣除的金额") @PathVariable("money") Integer money) {
        log.info("-----根据id修改用户余额555555555555");
        userService.deductionMoneyById01(id, money); //调用service
    }

    @ApiOperation("根据复杂条件批量查询用户06")
    @GetMapping("/list")
//    @GetMapping("{ids}")
    public List<UserVO> QueryUsers(UserQuery userQuery) {
        log.info("-----根据id查询用户6666666666666");
        List<User> users = userService.queryUsers(userQuery.getName(),
                userQuery.getStatus(),
                 userQuery.getMinBalance(),
                 userQuery.getMaxBalance()); //调用service
        System.out.println(users);
        return BeanUtil.copyToList(users, UserVO.class);
    }

    @ApiOperation("简单分页查询07")
    @GetMapping("/page")
    public PageDTO<UserVO> queryUserPage(UserQuery userQuery) {
        return userService.queryUserPage(userQuery); //调用service
    }

}
